﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UniRlv.COL;
using UniRlv.Utility;

namespace UniRlv.UI.Main.BasicInfo
{
    public partial class FrmCompanyInfo : UniRlv.UI.Main.FrmBase
    {
        public FrmCompanyInfo()
        {
            InitializeComponent();
        }

        private void FrmCompanyInfo_Load(object sender, EventArgs e)
        {
            toolBtnModify.Visible = true;
            toolBtnSync.Visible = true;

            toolBtnModify.Text = "保存";

            toolBtnModify.Click += toolBtnModify_Click;
            toolBtnSync.Click += toolBtnSync_Click;

            ShowObject(PublicObject.CurCompany);
        }

        void ShowObject(CompanyInfo c)
        {
            txtCompany.Text = c.name;
            txtOperator.Text = c.userName;
            txtPDAID.Text = c.userID;
            txtPDAPwd.Text = c.password;
            txtRole.Text = c.role;
            txtType.Text = string.Format("{0:D2}", c.type);
            txtPhone.Text = c.phone;
        }

        void toolBtnSync_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtPDAID.Text) || string.IsNullOrEmpty(txtPDAPwd.Text))
            {
                MessageBox.Show("PDA用户ID或密码不能为空");
                return;
            }

            try
            {
                Wait();

                var info = SyzsAsync.SyzsInfoAsync.GetUserInfo(txtPDAID.Text, txtPDAPwd.Text);

                if (info==null || string.IsNullOrEmpty(info.unit))
                {
                    ShowErrorMessageBox("同步失败", "用户名或密码不正确，无法获取企业信息！");
                    return;
                }

                PublicObject.CurCompany.companyID = info.unit;
                PublicObject.CurCompany.name = info.unitname;
                PublicObject.CurCompany.password = info.userpwd;
                PublicObject.CurCompany.role = info.rolename;
                PublicObject.CurCompany.type = Convert.ToInt32(info.unittype);
                PublicObject.CurCompany.userID = info.userid;
                PublicObject.CurCompany.name = info.unitname;
                PublicObject.CurCompany.userName = info.username;
                
                string msg = string.Empty;
                string sql = EntityHelper.GetUpdateSql(PublicObject.CurCompany);

                if (DALOpr.ExecuteNonQuery(sql, ref msg))
                {
                    ShowObject(PublicObject.CurCompany);
                    ShowInfoMessageBox("提示", "同步成功");
                }
                else
                {
                    ShowErrorMessageBox("同步失败", msg);
                }

                WaitBack();
            }
            catch (Exception exp)
            {
                ShowErrorMessageBox(exp);
            }
        }

        void toolBtnModify_Click(object sender, EventArgs e)
        {
            PublicObject.CurCompany.name = txtCompany.Text;
            PublicObject.CurCompany.userName = txtOperator.Text;
            PublicObject.CurCompany.userID = txtPDAID.Text;
            PublicObject.CurCompany.password = txtPDAPwd.Text;
            PublicObject.CurCompany.role = txtRole.Text;
            PublicObject.CurCompany.type = Convert.ToInt32(txtType.Text);
            PublicObject.CurCompany.phone = txtPhone.Text;

            string msg = string.Empty;
            string sql = EntityHelper.GetUpdateSql(PublicObject.CurCompany);

            if(DALOpr.ExecuteNonQuery(sql, ref msg))
            {
                ShowInfoMessageBox("提示", "修改成功");
            }
            else
            {
                ShowErrorMessageBox("出现错误", msg);
            }
        }
    }
}
